package com.jkt.rms.validator;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.jkt.rms.commons.utils.StringUtils;
import com.jkt.rms.constants.IConstants;
import com.jkt.rms.vo.VendorVO;
public class VendorValidator implements Validator {
	@SuppressWarnings("unchecked")
	@Override
	public boolean supports(Class clazz) {
		return clazz.equals(VendorVO.class);
	}
	public void validate(Object obj, Errors errors) {
		VendorVO vendorVO = (VendorVO) obj;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "vendorName", "error.mandatory.all.field", new String[]{"Vendor Name"});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "vendorCode", "error.mandatory.all.field", new String[]{"Vendor Code"});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "addressLine1", "error.mandatory.all.field", new String[]{"Address Line1"});
		/*if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "addressLine2", "error.mandatory.all.field", new String[]{"Address Line2"});*/
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "town","error.mandatory.all.field", new String[]{"Town"});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "county","error.mandatory.all.field", new String[]{"County"});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "country","error.mandatory.all.field", new String[]{"Country"});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "contactNo","error.mandatory.all.field", new String[]{"ContactNo"});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email","error.mandatory.all.field", new String[]{"Email"});
		if(errors.getErrorCount()!=0)return;
		//Offshore
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "offshorePersonName","error.mandatory.all.field", new String[]{"OffShore Person Name"});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "offshoreTown","error.mandatory.all.field", new String[]{"OffShore Town"});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "offshoreCounty","error.mandatory.all.field", new String[]{"OffShore County"});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "offshoreCountry","error.mandatory.all.field", new String[]{"OffShore Country"});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "offshoreContactNo","error.mandatory.all.field", new String[]{"OffShore Contact No."});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "offshoreEmail","error.mandatory.all.field", new String[]{"OffShore Email"});
		if(errors.getErrorCount()!=0)return;
		//onsite
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "onsitePersonName","error.mandatory.all.field", new String[]{"Onsite Person Name"});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "onsiteTown","error.mandatory.all.field", new String[]{"Onsite Town"});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "onsiteCounty","error.mandatory.all.field", new String[]{"Onsite County"});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "onsiteCountry","error.mandatory.all.field", new String[]{"Onsite Country"});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "onsiteContactNo","error.mandatory.all.field", new String[]{"Onsite Contact No."});
		if(errors.getErrorCount()!=0)return;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "onsiteEmail","error.mandatory.all.field", new String[]{"Onsite Email"});
		if(errors.getErrorCount()!=0)return;
		//validation to check field should be alphanumeric only
		if(vendorVO.getVendorName()!= null && !StringUtils.isAlphaNumericWithSpace(vendorVO.getVendorName())){
			errors.rejectValue("vendorName", "error.alphanumeric.field", new String[]{"Vendor Name"}, null);
		}
		if(vendorVO.getVendorCode()!= null && !StringUtils.isAlphaNumeric(vendorVO.getVendorCode())){
			errors.rejectValue("vendorCode", "error.alphanumeric.field", new String[]{"Vendor Code"}, null);
		}
		if(vendorVO.getTown()!= null && !StringUtils.isAlpha(vendorVO.getTown())){
			errors.rejectValue("town", "error.alpha.field", new String[]{"Town"},null);
		}
		if(vendorVO.getCounty()!= null && !StringUtils.isAlpha(vendorVO.getCounty())){
			errors.rejectValue("county", "error.alpha.field", new String[]{"county"},null);
		}
		if(vendorVO.getCountry()!= null && !StringUtils.isAlpha(vendorVO.getCountry())){
			errors.rejectValue("country", "error.alpha.field", new String[]{"country"},null);
		}
		if(vendorVO.getPostcode()!= null && !StringUtils.isAlphaNumericAndHyphen(vendorVO.getPostcode())){
			errors.rejectValue("postCode", "error.alphaNumerichyphen.field", new String[]{"PostCode"}, null);
		}
		//Offshore Person Details
		if(vendorVO.getOffshorePersonName()!= null && !StringUtils.isAlphaWithSpace(vendorVO.getOffshorePersonName())){
			errors.rejectValue("offshorePersonName", "error.alpha.field", new String[]{"OffShore Person Name"}, null);
		}
		
		if(vendorVO.getOffshoreTown()!= null && !StringUtils.isAlpha(vendorVO.getOffshoreTown())){
			errors.rejectValue("offshoreTown", "error.alpha.field", new String[]{"OffShore Town"}, null);
		}
		
		if(vendorVO.getOffshoreCounty()!= null && !StringUtils.isAlpha(vendorVO.getOffshoreCounty())){
			errors.rejectValue("offshoreCounty", "error.alpha.field", new String[]{"OffShore County"}, null);
		}
		if(vendorVO.getOffshoreCountry()!= null && !StringUtils.isAlpha(vendorVO.getOffshoreCountry())){
			errors.rejectValue("offshoreCountry", "error.alpha.field", new String[]{"OffShore Country"},null);
		}
		if(vendorVO.getOffshorePostCode()!= null && !StringUtils.isAlphaNumericAndHyphen(vendorVO.getOffshorePostCode())){
			errors.rejectValue("offshorePostCode", "error.alphaNumerichyphen.field", new String[]{"Offshore PostCode"}, null);
		}
		//Onsite Person Details
		if(vendorVO.getOnsitePersonName()!= null && !StringUtils.isAlphaWithSpace(vendorVO.getOnsitePersonName())){
			errors.rejectValue("onsitePersonName", "error.alpha.field", new String[]{"Onsite Person Name"}, null);
		}
		if(vendorVO.getOnsiteTown()!= null && !StringUtils.isAlpha(vendorVO.getOnsiteTown())){
			errors.rejectValue("onsiteTown", "error.alpha.field", new String[]{"Onsite Town"}, null);
		}
		if(vendorVO.getOnsiteCounty()!= null && !StringUtils.isAlpha(vendorVO.getOnsiteCounty())){
			errors.rejectValue("onsiteCounty", "error.alpha.field", new String[]{"Onsite County"}, null);
		}
		if(vendorVO.getOnsiteCountry()!= null && !StringUtils.isAlpha(vendorVO.getOnsiteCountry())){
			errors.rejectValue("onsiteCountry", "error.alpha.field", new String[]{"Onsite Country"},null);
		}
		if(vendorVO.getOnsitePostCode()!= null && !StringUtils.isAlphaNumericAndHyphen(vendorVO.getOnsitePostCode())){
			errors.rejectValue("OnsitePostCode", "error.alphaNumerichyphen.field", new String[]{"Onsite PostCode"}, null);
		}
		//validation to check fields maximum length
		if(vendorVO.getVendorName()!= null && !StringUtils.isGreaterThan(vendorVO.getVendorName(),100)){
			errors.rejectValue("vendorName", "error.maxlength.field", new String[]{"Vendor Name","100"}, null);
		}
		if(vendorVO.getVendorCode()!= null && !StringUtils.isGreaterThan(vendorVO.getVendorCode(),8)){
			errors.rejectValue("vendorCode", "error.maxlength.field", new String[]{"Vendor Code","8"}, null);
		}
		if(vendorVO.getAddressLine1()!= null && !StringUtils.isGreaterThan(vendorVO.getAddressLine1(),100)){
			errors.rejectValue("addressLine1", "error.maxlength.field", new String[]{"Address Line 1","100"}, null);
		}
		if(vendorVO.getAddressLine2()!= null && !StringUtils.isGreaterThan(vendorVO.getAddressLine2(),100)){
			errors.rejectValue("addressLine2", "error.maxlength.field", new String[]{"Address Line 2","100"}, null);
		}
		if(vendorVO.getTown()!= null && !StringUtils.isGreaterThan(vendorVO.getTown(),100)){
			errors.rejectValue("town", "error.maxlength.field", new String[]{"Town","100"}, null);
		}
		if(vendorVO.getCounty()!= null && !StringUtils.isGreaterThan(vendorVO.getCounty(),100)){
			errors.rejectValue("county", "error.maxlength.field", new String[]{"County","100"}, null);
		}
		if(vendorVO.getCountry()!= null && !StringUtils.isGreaterThan(vendorVO.getCountry(),100)){
			errors.rejectValue("country", "error.maxlength.field", new String[]{"Country","100"}, null);
		}
		if(vendorVO.getPostcode()!= null && !StringUtils.isGreaterThan(vendorVO.getPostcode(),50)){
			errors.rejectValue("postcode", "error.maxlength.field", new String[]{"PostCode","50"}, null);
		}
		if(vendorVO.getContactNo()!= null && !StringUtils.isGreaterThan(vendorVO.getContactNo(),20)){
			errors.rejectValue("contactNo", "error.maxlength.field", new String[]{"ContactNo","20"}, null);
		}
		if(vendorVO.getComments()!= null && !StringUtils.isGreaterThan(vendorVO.getComments(),400)){
			errors.rejectValue("comments", "error.maxlength.field", new String[]{"Comments","400"}, null);
		}
		//Offshore Details
		if(vendorVO.getOffshorePersonName()!= null && !StringUtils.isGreaterThan(vendorVO.getOffshorePersonName(),50)){
			errors.rejectValue("offshorePersonName", "error.maxlength.field", new String[]{"OffShore Person Name","50"}, null);
		}
		if(vendorVO.getOffshoreAddressLine1()!= null && !StringUtils.isGreaterThan(vendorVO.getOffshoreAddressLine1(),100)){
			errors.rejectValue("offshoreAddressLine1", "error.maxlength.field", new String[]{"OffShore Address Line 1","100"}, null);
		}

		if(vendorVO.getOffshoreAddressLine2()!= null && !StringUtils.isGreaterThan(vendorVO.getOffshoreAddressLine2(),100)){
			errors.rejectValue("offshoreAddressLine2", "error.maxlength.field", new String[]{"OffShore Address Line 2","100"}, null);
		}
		if(vendorVO.getOffshoreTown()!= null && !StringUtils.isGreaterThan(vendorVO.getOffshoreTown(),100)){
			errors.rejectValue("offshoreTown", "error.vendor.maxlength.field", new String[]{"OffShore Town","100"}, null);
		}
		if(vendorVO.getOffshoreCounty()!= null && !StringUtils.isGreaterThan(vendorVO.getOffshoreCounty(),100)){
			errors.rejectValue("offshoreCounty", "error.maxlength.field", new String[]{"OffShore County","100"}, null);
		}
		if(vendorVO.getOffshoreCountry()!= null && !StringUtils.isGreaterThan(vendorVO.getOffshoreCountry(),100)){
			errors.rejectValue("offshoreCountry", "error.maxlength.field", new String[]{"OffShore Country","100"}, null);
		}
		if(vendorVO.getOffshorePostCode()!= null && !StringUtils.isGreaterThan(vendorVO.getOffshorePostCode(),50)){
			errors.rejectValue("offshorePostCode", "error.maxlength.field", new String[]{"Offshore PostCode","50"}, null);
		}
		if(vendorVO.getOffshoreContactNo()!= null && !StringUtils.isGreaterThan(vendorVO.getOffshoreContactNo(),20)){
			errors.rejectValue("offshoreContactNo", "error.maxlength.field", new String[]{"OffShore ContactNo","20"}, null);
		}
		//onsite
		if(vendorVO.getOnsitePersonName()!= null && !StringUtils.isGreaterThan(vendorVO.getOnsitePersonName(),50)){
			errors.rejectValue("onsitePersonName", "error.maxlength.field", new String[]{"Onsite Person Name","50"}, null);
		}
		if(vendorVO.getOnsiteAddressLine1()!= null && !StringUtils.isGreaterThan(vendorVO.getOnsiteAddressLine1(),100)){
			errors.rejectValue("onsiteAddressLine1", "error.maxlength.field", new String[]{"OnSite Address Line 1","100"}, null);
		}
		if(vendorVO.getOnsiteAddressLine2()!= null && !StringUtils.isGreaterThan(vendorVO.getOnsiteAddressLine2(),100)){
			errors.rejectValue("onsiteAddressLine2", "error.maxlength.field", new String[]{"OnSite Address Line 2","100"}, null);
		}
		if(vendorVO.getOnsiteTown()!= null && !StringUtils.isGreaterThan(vendorVO.getOnsiteTown(),100)){
			errors.rejectValue("onsiteTown", "error.maxlength.field", new String[]{"Onsite Town","100"}, null);
		}
		if(vendorVO.getOnsiteCounty()!= null && !StringUtils.isGreaterThan(vendorVO.getOnsiteCounty(),100)){
			errors.rejectValue("onsiteCounty", "error.maxlength.field", new String[]{"Onsite County","100"}, null);
		}
		if(vendorVO.getOnsiteCountry()!= null && !StringUtils.isGreaterThan(vendorVO.getOnsiteCountry(),100)){
			errors.rejectValue("onsiteCountry", "error.maxlength.field", new String[]{"Onsite Country","100"}, null);
		}
		if(vendorVO.getOnsitePostCode()!= null && !StringUtils.isGreaterThan(vendorVO.getOnsitePostCode(),50)){
			errors.rejectValue("onsitePostCode", "error.maxlength.field", new String[]{"Onsite PostCode","50"}, null);
		}
		if(vendorVO.getOnsiteContactNo()!= null && !StringUtils.isGreaterThan(vendorVO.getOnsiteContactNo(),20)){
			errors.rejectValue("onsiteContactNo", "error.maxlength.field", new String[]{"Onsite ContactNo","20"}, null);
		}
		////validate contact Number
		if(vendorVO.getContactNo()!= null && !"".equals(vendorVO.getContactNo()) && !StringUtils.validate(vendorVO.getContactNo(),IConstants.CONTACTNO_PATTERN)){
			errors.rejectValue("contactNo","error.invalid", new String[]{"Contact No"}, null);
		}
		
		if(vendorVO.getOffshoreContactNo()!= null && !"".equals(vendorVO.getOffshoreContactNo()) && !StringUtils.validate(vendorVO.getOffshoreContactNo(),IConstants.CONTACTNO_PATTERN)){
			errors.rejectValue("offshoreContactNo","error.invalid", new String[]{"OffShore Contact No"}, null);
		}
		if(vendorVO.getOnsiteContactNo()!= null && !"".equals(vendorVO.getOnsiteContactNo()) && !StringUtils.validate(vendorVO.getOnsiteContactNo(),IConstants.CONTACTNO_PATTERN)){
			errors.rejectValue("onsiteContactNo","error.invalid", new String[]{"Onsite Contact No"}, null);
		}
		//validate email 
		if(vendorVO.getEmail()!= null && !"".equals(vendorVO.getEmail()) && !StringUtils.validate(vendorVO.getEmail(),IConstants.EMAIL_PATTERN)){
			errors.rejectValue("email", "error.invalid", new String[]{"Email"}, null);
		}
		if(vendorVO.getOffshoreEmail()!= null && !"".equals(vendorVO.getOffshoreEmail()) && !StringUtils.validate(vendorVO.getOffshoreEmail(),IConstants.EMAIL_PATTERN)){
			errors.rejectValue("offshoreEmail", "error.invalid", new String[]{"OffShore Email"}, null);
		}
		if(vendorVO.getOnsiteEmail()!= null && !"".equals(vendorVO.getOnsiteEmail()) && !StringUtils.validate(vendorVO.getOnsiteEmail(),IConstants.EMAIL_PATTERN)){
			errors.rejectValue("onsiteEmail", "error.invalid", new String[]{"Onsite Email"}, null);
		}
		//Blended Rate Validation
		if(vendorVO.getOffshoreBlendedRate()!= 0 && !StringUtils.validate(Float.toString(vendorVO.getOffshoreBlendedRate()),IConstants.BLENDED_RATE_PATTERN)){
			errors.rejectValue("offshoreBlendedRate", "error.invalid", new String[]{"Blended Rate Offshore"}, null);
		}
		if(vendorVO.getOnsiteBlendedRate()!= 0 && !StringUtils.validate(Float.toString(vendorVO.getOnsiteBlendedRate()),IConstants.BLENDED_RATE_PATTERN)){
			errors.rejectValue("onsiteBlendedRate", "error.invalid", new String[]{"Blended Rate Onsite"}, null);
		}
		
	}
}
